<template>
  <el-dialog
    :title="!dataForm.catIdentify ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="猫的编号" prop="catRepellentId">
      <el-input v-model="dataForm.catRepellentId" placeholder="猫的编号"></el-input>
    </el-form-item>
    <el-form-item label="驱虫日期" prop="catRepellentDate">
      <el-input v-model="dataForm.catRepellentDate" placeholder="驱虫日期"></el-input>
    </el-form-item>
    <el-form-item label="驱虫地址" prop="catRepellentAddress">
      <el-input v-model="dataForm.catRepellentAddress" placeholder="驱虫地址"></el-input>
    </el-form-item>
    <el-form-item label="驱虫类型" prop="catRepellentType">
      <el-input v-model="dataForm.catRepellentType" placeholder="驱虫类型"></el-input>
    </el-form-item>
    <el-form-item label="驱虫单位" prop="catRepellentUnit">
      <el-input v-model="dataForm.catRepellentUnit" placeholder="驱虫单位"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="catRepellentCarer">
      <el-input v-model="dataForm.catRepellentCarer" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="catRepellentDesc">
      <el-input v-model="dataForm.catRepellentDesc" placeholder="描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          catIdentify: 0,
          catRepellentId: '',
          catRepellentDate: '',
          catRepellentAddress: '',
          catRepellentType: '',
          catRepellentUnit: '',
          catRepellentCarer: '',
          catRepellentDesc: ''
        },
        dataRule: {
          catRepellentId: [
            { required: true, message: '猫的编号不能为空', trigger: 'blur' }
          ],
          catRepellentDate: [
            { required: true, message: '驱虫日期不能为空', trigger: 'blur' }
          ],
          catRepellentAddress: [
            { required: true, message: '驱虫地址不能为空', trigger: 'blur' }
          ],
          catRepellentType: [
            { required: true, message: '驱虫类型不能为空', trigger: 'blur' }
          ],
          catRepellentUnit: [
            { required: true, message: '驱虫单位不能为空', trigger: 'blur' }
          ],
          catRepellentCarer: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          catRepellentDesc: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.catIdentify = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.catIdentify) {
            this.$http({
              url: this.$http.adornUrl(`/cat/repellentrecord/info/${this.dataForm.catIdentify}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.catRepellentId = data.repellentRecord.catRepellentId
                this.dataForm.catRepellentDate = data.repellentRecord.catRepellentDate
                this.dataForm.catRepellentAddress = data.repellentRecord.catRepellentAddress
                this.dataForm.catRepellentType = data.repellentRecord.catRepellentType
                this.dataForm.catRepellentUnit = data.repellentRecord.catRepellentUnit
                this.dataForm.catRepellentCarer = data.repellentRecord.catRepellentCarer
                this.dataForm.catRepellentDesc = data.repellentRecord.catRepellentDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cat/repellentrecord/${!this.dataForm.catIdentify ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'catIdentify': this.dataForm.catIdentify || undefined,
                'catRepellentId': this.dataForm.catRepellentId,
                'catRepellentDate': this.dataForm.catRepellentDate,
                'catRepellentAddress': this.dataForm.catRepellentAddress,
                'catRepellentType': this.dataForm.catRepellentType,
                'catRepellentUnit': this.dataForm.catRepellentUnit,
                'catRepellentCarer': this.dataForm.catRepellentCarer,
                'catRepellentDesc': this.dataForm.catRepellentDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
